using DataFrames
using QuadGK
using Distributions
using JLD
using Optim
using ForwardDiff
using CSV
using LinearAlgebra
using SpecialFunctions
using Random
using DelimitedFiles
using SparseArrays

clearconsole()

#-------------------------------------------------------------
# include Functions
#-------------------------------------------------------------
#cd("$(pwd())/Dropbox/Heterogeneity/Software/KS_Simulation/")
readDir = "$(pwd())/Functions/"
# include(readDir *"vech.jl");
#include(readDir *"VAR_Procedures.jl");
include(readDir *"VAR_MoreLags_Procedures.jl");
include(readDir *"Loaddata.jl");

#-------------------------------------------------------------
# choose specification files
#-------------------------------------------------------------
nVARMDDSpec = "1"
specDir     = "$(pwd())/SpecFiles/"
include(specDir * "/AggVARMDDspec" * nVARMDDSpec * ".jl")

#-------------------------------------------------------------
# load aggregate data
#-------------------------------------------------------------
dataDir  = "$(pwd())/data/"
agg_data, period_agg, ~ = loadaggdata(SampleStart,SampleEnd)
n_agg    = size(agg_data)[2]
n = n_agg

#-------------------------------------------------------------
# Compute MDD over hyperparameter grid
#-------------------------------------------------------------

MDD_vec = zeros(hyper_n)

for ll = 1:hyper_n

    # Extract hyperparaters
    lambda1 = hyper_grid[ll,1]
    lambda2 = hyper_grid[ll,2]
    lambda3 = hyper_grid[ll,3]
    lambda4 = 2
    lambda  = [lambda1 lambda2 lambda3 lambda4]
    nlags   = Int(hyper_grid[ll,4])

    # Adjust the sample
    data_adj = agg_data[Tdrop-nlags+1:end,:]

    # OLS estimation
    YY, XX, PHIols, Sols, SIGMAols = data_to_YY(data_adj, nlags, nlags)

    # Generate Prior
    igammadf    = size(YY)[2]+sigdf
    prior       = prior_ACP_stru(YY, SIGMAols, n_agg, igammadf, nlags, lambda)

    # Compute MDD
    MDD_vec[ll] = ml_VAR_ACP(nlags,YY,XX,prior)

end


#-------------------------------------------------------------
# Save output
#-------------------------------------------------------------

sName    = "AggVAR_MDD" * nVARMDDSpec
savedir  = "$(pwd())/results/" * sName *"/";
try mkdir(savedir) catch; end

lambda_MDD = [hyper_grid MDD_vec]
sNameCols  = ["Lam1", "Lam2", "Lam3", "Nlags", "MDD"]
sNameFile  = "AggVAR_MDD" * nVARMDDSpec
CSV.write(savedir * sNameFile * "_lambda_MDD.csv", DataFrame(permutedims(sNameCols),:auto), header=false)
CSV.write(savedir * sNameFile * "_lambda_MDD.csv", DataFrame(lambda_MDD,:auto), append=true);
